//
//  DetailCerCardViewController.m
//  CreditHub
//
//  Created by chenglang on 2024/9/20.
//

#import "DetailCerCardViewController.h"
#import "DetailCerCardTableViewCell.h"
#import "HomeRequest.h"
#import "PanPopupViewController.h"
#import "CardTipPopupViewController.h"
#import "PanPopupConfirmViewController.h"

@interface DetailCerCardViewController ()<UITableViewDelegate,UITableViewDataSource,SelectImageDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *buttonNext;
@property (nonatomic , strong) NSDictionary *dicHumming;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutBottom;

@property (nonatomic,strong) NSArray *arrayPanPar;
@property (nonatomic, strong) UIImage *imagePan;
@property (nonatomic, strong) UIImage *imageCard;

@end

@implementation DetailCerCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    self.ch_title = @"Identity authentication";
    self.navBgImageView.hidden = NO;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailCerCardTableViewCell" bundle:nil] forCellReuseIdentifier:@"DetailCerCardTableViewCell"];
    
    [self requestDetail];
}

- (void)requestDetail{
    __weak typeof(self) weakSelf = self;
    [HomeRequest requestCardInfoId:self.lightat success:^(NSURLSessionDataTask *task, id response) {
        if([response[@"ofexcitement"] integerValue] == 0){
            weakSelf.dicHumming = [NSDictionary dictionaryWithDictionary:response[@"pity"][@"humming"]];
            if(!CHStringIsEmpty(weakSelf.dicHumming[@"forletting"])&&!CHStringIsEmpty(weakSelf.dicHumming[@"haven"])){
                weakSelf.buttonNext.hidden = YES;
                weakSelf.layoutBottom.constant = 0;
            }
            [weakSelf.tableView reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailCerCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCerCardTableViewCell"];
    [cell.buttonCard addTarget:self action:@selector(clickCard:) forControlEvents:UIControlEventTouchUpInside];
    [cell.buttonPan addTarget:self action:@selector(clickPan:) forControlEvents:UIControlEventTouchUpInside];
    cell.dicHumming = self.dicHumming;
    if(self.imagePan&&CHStringIsEmpty(self.dicHumming[@"haven"])){
        cell.imageViewPan.image = self.imagePan;
        cell.imageViewPanStatus.hidden = NO;
    }
    if(self.imageCard&&CHStringIsEmpty(self.dicHumming[@"forletting"])){
        cell.imageViewCard.image = self.imageCard;
        cell.imageViewCardStatus.hidden = NO;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 720;
    
}

- (IBAction)clickPan:(id)sender {
    
    self.startTime = [BaseService getTimeStringSince1970];
    __weak typeof(self) weakSelf = self;
    PanPopupViewController *vc = [[PanPopupViewController alloc] init];
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    vc.okblock = ^{
        [weakSelf openCameraOrGalleryType:@"13"];

    };
    [self presentViewController:vc animated:YES completion:nil];
    
    
    
}
- (void)clickCard:(id)sender {
    
    self.startTime = [BaseService getTimeStringSince1970];
    __weak typeof(self) weakSelf = self;
    CardTipPopupViewController *vc = [[CardTipPopupViewController alloc] init];
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    vc.okblock  = ^{
        [weakSelf selectImage:UIImagePickerControllerSourceTypeCamera type:@"10"];
    };
    [self presentViewController:vc animated:YES completion:nil];
    
}

- (void)selectedFinishTaskWithResultImage:(UIImage *)image sourceType:(NSInteger)sourceType type:(NSString *)type{
    
    [self uploadImage:image spindly:type awfully:sourceType];
}
- (void)uploadImage:(UIImage *)image spindly:(NSString *)spindly awfully:(NSInteger)awfully{
    xhl_hudLoading();
    __weak typeof(self) weakSelf = self;
    [HomeRequest requestUploadImage:image spindly:spindly awfully:awfully success:^(NSURLSessionDataTask *task, id response) {
        
        if([response[@"ofexcitement"] integerValue] == 0){
            
            if([spindly isEqualToString:@"10"]){
                // card
                weakSelf.imageCard = image;
                [weakSelf.tableView reloadData];
                if(weakSelf.imagePan){
                    [weakSelf.buttonNext setTitle:@"Confirm And Submit" forState:UIControlStateNormal];
                }
                weakSelf.pointType = @"5";
                weakSelf.endTime = [BaseService getTimeStringSince1970];
                [weakSelf requestUploadPointType];
            }else{
                //pan
                PanPopupConfirmViewController *vcPna = [[PanPopupConfirmViewController alloc] init];
                vcPna.modalPresentationStyle = UIModalPresentationOverFullScreen;
                vcPna.lightat = weakSelf.lightat;
                vcPna.arrayInfo = [NSMutableArray arrayWithArray:response[@"pity"][@"lecture"]];
                vcPna.confirmBlock = ^(NSArray * _Nonnull array) {
                    weakSelf.imagePan = image;
                    weakSelf.arrayPanPar = array;
                    [weakSelf.tableView reloadData];
                    if(weakSelf.imageCard){
                        [weakSelf.buttonNext setTitle:@"Confirm And Submit" forState:UIControlStateNormal];
                    }
                  
                };
                [weakSelf presentViewController:vcPna animated:YES completion:nil];
            }
            
        }
        xhl_showMessage(response[@"shared"]);
      
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        xhl_hudDismiss();
        xhl_showMessage(@"upload failed");
    }];
}

- (IBAction)clickNext:(id)sender {
    if(!self.imagePan&&CHStringIsEmpty(self.dicHumming[@"haven"])){
        [self clickPan:nil];
    }else if (!self.imageCard&&CHStringIsEmpty(self.dicHumming[@"forletting"])){
        [self clickCard:nil];
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
